<?php
namespace Common\Controller;
use Common\Controller\AppframeController;
use Home\Service\WechatService;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2017年2月17日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class HomebaseController extends AppframeController{
    protected $user_sn = '';
    protected $user_uid = '';
    protected $userInfo='';
    protected $model='';
    protected $applyType=[];
    protected $approver = false;
    protected $goback = '';
    protected $attInfo = '';//考勤信息
    protected $user_openid='';
    protected $account = '';//account API类
    
    public function __construct() {
        parent::__construct();
        $this->_check_user();
        /*获取更新openid*/
        
        if (!$this->user_openid) {
           $this->_authorize();//进行微信授权登陆
        }
        
        $this->account = new AccountService();
        
        $this->applyType = C('APPLY_TYPE');
        $this->attInfo = $this->_att_info();
        $this->_check_sucking();//检查是否哺乳期
        $this->assign('goback',U('Index/index'));
        $this->assign('user_info',$this->userInfo);//用户信息
        $this->assign('att_info',$this->attInfo);//获取 缺勤时间，补休时间
        $this->_approver();//身份检查
    }
    
    
    /**
     * 授权登录，获取openid
     *
     * date:2017年3月13日
     * author: EK_熊
     */
    private function _authorize(){
        $session_user = session('user');
        if ($session_user['wx_openid']){
            $this->user_openid = $session_user['wx_openid'];
        }else{
            $wechat = new WechatService();
            $wechat->authorize(5);
        }
    }

    
    
    /**
     * 获取审核身份
     * 
     * date:2017年2月27日
     * author: EK_熊
     */
    private function _approver(){
        $attCfg = D('Options')->getOption('att_base',true);
        $approverlist = $this->account->get_approver_sn();
        if (in_array($this->user_sn,$approverlist)|| $this->user_sn == $attCfg['hr_sn'] || $this->user_sn == $attCfg['admin_sn' ]|| $this->user_sn == $attCfg['admin_leader_sn']) {
            $this->approver = true;//主管
        }
    }
    
    /**
     * 获取当前用户的考勤数据，
     * 最近60天的补休时间，本月的缺勤时间
     * 
     * 
     * date:2017年3月1日
     * author: EK_熊
     */
    private function _att_info(){
        $attDayModel = D('AttendanceDay');
        $month_start = date('Y-m-01', strtotime(date("Y-m-d")));//当月开始第一天
        $lastday_end = date('Y-m-d', strtotime("-1 day"));

        $month_pre_end =date("Y-m-d",mktime(23,59,59,date("m"),0,date("Y")));//上一个月结束时间
        $month_pre_start = date("Y-m-01",strtotime('-1 month'));
        
        $start_from_60 = date("Y-m-d",strtotime("-60 day"));//60天以前
        
        $overtime = $attDayModel->count_remain_overtime($this->user_sn,$start_from_60,$lastday_end);//时间范围：从往前60天时间~当天时间

        $annual = D('AnnualLeave')->remain_annual($this->user_sn);
        //旷工
        $kuanggong = $attDayModel->_absenteeism_num_no_data($this->user_sn,$month_start,$lastday_end);
        if ($lastday_end < $month_start) {
            $lastday_end = $month_start;//跨月份,月头1号，显示不全勤
            $absent_cur=0;
        }else{
            /*缺勤数：本月1号至昨天的应出勤分钟数- 入职/离职时间- 期间全部假期时间 -本月1号至昨天的实际出勤数*/
           $absent_cur = $attDayModel->get_absent_mon($this->user_sn,$month_start,$lastday_end);
           $absent_cur = $absent_cur - $kuanggong['min'];
        }
        
        //已请事假
        $map_apply['sn']=$this->user_sn;
        $map_apply['date']=['between',[$month_start,$lastday_end]];
        $leave_casual = $attDayModel->where($map_apply)->sum('leave_casual');
        $userAttInfo = [
            'overtime'=>$overtime > 0 ? $overtime: 0,//可补休时间
            'absent_cur'=>$absent_cur>0 ?$absent_cur : 0,
            'annual'=>time_day_8hour_zh($annual['total']),
            'annual_min'=>$annual['total'],
            'leave_casual'=>$leave_casual,
            'kuanggong'=>$kuanggong['min']
        ];
        if ($annual['adjust'] >0) {
            $userAttInfo['annual_adjust']=time_day_8hour_zh($annual['adjust']);
        }
        
        //剩余病假数
        $annualModel = D('AnnualLeave');
        $sick_full_remain = $annualModel->remain_sick($this->user_sn,'109');
        $sick_half_remain = $annualModel->remain_sick($this->user_sn,'110');
        $sick_zh_full_remain = time_day_8hour_zh($sick_full_remain);
        $sick_zh_half_remain = time_day_8hour_zh($sick_half_remain);
        
        if ($sick_full_remain >0) $userAttInfo['leave_sick'] ="全日薪病假数:$sick_zh_full_remain";
        if ($sick_half_remain >0 && $sick_full_remain <=0) $userAttInfo['leave_sick'] ="半日薪病假数:$sick_zh_half_remain";
//         dump($userAttInfo);
        return $userAttInfo;
    }
    
    //哺乳期检查
    private function _check_sucking(){
        
        $this->userInfo['suckling'] = $suckling = $this->account->suckling($this->user_sn);
        $this->userInfo['suckling']['total_min'] = $suckling['work_hour']*60;//平均每天的上班时间
        
        $curDate = date('Y-m-d');
        if ($suckling && ($curDate >= $suckling['date_start'] && $curDate <= $suckling['date_end'])) {
            $this->userInfo['suckling']['show'] = true;
        }
    }
    
    
    /**
     * 用户登录检查
     *
     * date:2017年1月5日
     * author: EK_熊
     */
    private function _check_user(){
        $userInfo = get_login_user();
        if (!$userInfo) {
            $this->redirect('Public/login',['jumpurl'=>base64_encode($_SERVER['REQUEST_URI'])]);
        }
        $userInfo['truename'] = $userInfo['name'];
        if ($userInfo['leader_id'] == 0) {
            $option_att_base = F('att_base');
            $userInfo['approver_name'] = $option_att_base['admin_name'];
        }
        $this->userInfo = $userInfo;
        $this->user_sn = $userInfo['sn'];
        $this->user_uid = $userInfo['id'];
        $this->user_openid = $userInfo['wx_openid'];
    }
    
    public function _empty(){
        //把所有城市的操作解析到city方法
        $this->display("html/".ACTION_NAME);
        exit;
    }
    
}